import scrapy
from ..items import ImgtestspiderItem

class ImgtestSpiderSpider(scrapy.Spider):
    name = 'imgTest_spider'
    # allowed_domains = ['baidu.com']
    start_urls = ['https://www.qqtn.com/tp/wmtp_1.html']
    format_url = 'https://www.qqtn.com/tp/wmtp_{}.html'
    page = 1
    def parse(self, response):
        print(response.text)
        imgs = response.xpath('//ul[@class="g-gxlist-imgbox"]//li/a/img')
        for img  in imgs:
            src = img.xpath('@src').get()
            title = img.xpath('@alt').get()

            print(src, title)
            yield scrapy.Request(url=src, callback=self.deep_parse, meta={'a':title})
        if self.page<3:
            self.page += 1
            yield scrapy.Request(url=self.format_url.format(self.page))
    def deep_parse(self,response):
        item = ImgtestspiderItem()
        item['img_content'] = response.body
        item['name'] = response.meta['a']
        yield item